<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Refrensi_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	//==================================================================
	function get_jenis_buku(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_jenis_buku a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_jenis_buku a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_jenis_buku($data){
		$this->db->insert('tb_perpus_jenis_buku',$data);
	}
	function validasi_kode_jenis_buku($kode){
		$this->db->select('count(*) as jml, ID_JENIS');
		$this->db->from('tb_perpus_jenis_buku');  
		$this->db->where('kode_jenis',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_jenisID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_jenis_buku a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_JENIS',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_jenis($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_JENIS',$kode);
		$this->db->update('tb_perpus_jenis_buku',$data);
	}
	
	function delete_jenis($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_JENIS',$kode);
		$this->db->delete('tb_perpus_jenis_buku'); 
	}
	
	function get_list_jenis_buku(){
		$this->db->select('*');
		$this->db->from('tb_perpus_jenis_buku');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	
	//=================================================================================
	function get_penerbit(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_penerbit a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_penerbit a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_penerbit($data){
		$this->db->insert('tb_perpus_penerbit',$data);
	}
	function validasi_id_penerbit($kode){
		$this->db->select('count(*) as jml, ID_PENERBIT');
		$this->db->from('tb_perpus_penerbit');  
		$this->db->where('KODE_PENERBIT',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_penerbitID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_penerbit a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_PENERBIT',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_penerbit($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_PENERBIT',$kode);
		$this->db->update('tb_perpus_penerbit',$data);
	}
	
	function delete_penerbit($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_PENERBIT',$kode);
		$this->db->delete('tb_perpus_penerbit'); 
	}
	
	function get_list_penerbit(){
		$this->db->select('*');
		$this->db->from('tb_perpus_penerbit');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	//=================================================================================
	function get_penulis(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_penulis a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_penulis a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_penulis($data){
		$this->db->insert('tb_perpus_penulis',$data);
	}
	function validasi_id_penulis($kode){
		$this->db->select('count(*) as jml, ID_PENULIS');
		$this->db->from('tb_perpus_penulis');  
		$this->db->where('KODE_PENULIS',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_penulisID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_penulis a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_PENULIS',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_penulis($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_PENULIS',$kode);
		$this->db->update('tb_perpus_penulis',$data);
	}
	
	function delete_penulis($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_PENULIS',$kode);
		$this->db->delete('tb_perpus_penulis'); 
	}
	
	function get_list_penulis(){
		$this->db->select('*');
		$this->db->from('tb_perpus_penulis');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	//=================================================================================
	function get_rak(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_rak a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_rak a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_rak($data){
		$this->db->insert('tb_perpus_rak',$data);
	}
	function validasi_id_rak($kode){
		$this->db->select('count(*) as jml, ID_RAK');
		$this->db->from('tb_perpus_rak');  
		$this->db->where('KODE_RAK',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_rakID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_rak a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_RAK',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_rak($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_RAK',$kode);
		$this->db->update('tb_perpus_rak',$data);
	}
	
	function delete_rak($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_RAK',$kode);
		$this->db->delete('tb_perpus_rak'); 
	}
	
	function get_list_rak(){
		$this->db->select('*');
		$this->db->from('tb_perpus_rak');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	//=================================================================================
	function get_denda(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, d.*');
		$this->db->from('tb_perpus_denda a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_pinjam c','c.ID_PINJAM=a.ID_PINJAM');
		$this->db->join('tb_siswa d','d.NISN=c.NISN');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, d.*');
		$this->db->from('tb_perpus_denda a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_pinjam c','c.ID_PINJAM=a.ID_PINJAM');
		$this->db->join('tb_siswa d','d.NISN=c.NISN');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_denda($data){
		$this->db->insert('tb_perpus_denda',$data);
	}
	function validasi_id_denda($kode){
		$this->db->select('count(*) as jml, ID_DENDA');
		$this->db->from('tb_perpus_denda');  
		$this->db->where('ID_JENIS',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_dendaID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK, c.*');
		$this->db->from('tb_perpus_denda a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_jenis_buku c','c.ID_JENIS=a.ID_JENIS');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_DENDA',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_denda($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_DENDA',$kode);
		$this->db->update('tb_perpus_denda',$data);
	}
	
	function delete_denda($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_DENDA',$kode);
		$this->db->delete('tb_perpus_denda'); 
	}
	
	//=================================================================================
	function get_katalog($kode){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_katalog a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_RAK',$kode);
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_katalog a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_RAK',$kode);
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_katalog($data){
		$this->db->insert('tb_perpus_katalog',$data);
	}
	function validasi_id_katalog($kode){
		$this->db->select('count(*) as jml, ID_KATALOG');
		$this->db->from('tb_perpus_katalog');  
		$this->db->where('KODE_KATALOG',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_katalogID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK');
		$this->db->from('tb_perpus_katalog a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_KATALOG',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_katalog($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_KATALOG',$kode);
		$this->db->update('tb_perpus_katalog',$data);
	}
	
	function delete_katalog($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_KATALOG',$kode);
		$this->db->delete('tb_perpus_katalog'); 
	}
	
	function get_list_katalog(){
		$this->db->select('*');
		$this->db->from('tb_perpus_katalog');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	
	//=================================================================================
	function get_konfigurasi(){
		$this->db->select('*');
		$this->db->from('tb_perpus_konfigurasi a');
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_perpus_konfigurasi a');
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_konfigurasi($data){
		$this->db->insert('tb_perpus_konfigurasi',$data);
	}
	function validasi_konfigurasi($anggota){
		$this->db->select('count(*) as jml, ID_KONFIGURASI');
		$this->db->from('tb_perpus_konfigurasi');  
		$this->db->where('JENIS_ANGGOTA',$anggota);
		$query = $this->db->get();
		return $query;
	}
	function get_konfigurasiID($kode){
		$this->db->select('*');
		$this->db->from('tb_perpus_konfigurasi a');  
		$this->db->where('a.ID_KONFIGURASI',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_konfigurasi($data, $kode){
		$this->db->where('ID_KONFIGURASI',$kode);
		$this->db->update('tb_perpus_konfigurasi',$data);
	}
	
	function delete_konfigurasi($kode){
		$this->db->where('ID_KONFIGURASI',$kode);
		$this->db->delete('tb_perpus_konfigurasi'); 
	}
}

?>
